﻿
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Configuration.UserSecrets;
using MyCompany.MyProject.Common;
using MyCompany.MyProject.Common.Helper;
using MyCompany.MyProject.Common.LogHelper;
using MyCompany.MyProject.Model;
using MyCompany.MyProject.WebApi.ModelDto;
using Newtonsoft.Json;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;

namespace MyCompany.MyProject.WebApi.Controllers
{

    /// <summary>
    /// 监控控制器
    /// </summary>
    public class MonitorController : BaseApiController
    {
        private readonly IWebHostEnvironment _env;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="environment"></param>
        public MonitorController(IWebHostEnvironment environment)
        {
            _env = environment;
        }

        /// <summary>
        /// 获取服务器配置信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public MessageModel<ServerViewModel> GetServiceInfo()
        {
            return Success(new ServerViewModel()
            {
                EnvironmentName = _env.EnvironmentName,
                OSArchitecture = RuntimeInformation.OSArchitecture.ObjToString(),
                ContentRootPath = _env.ContentRootPath,
                WebRootPath = _env.WebRootPath,
                FrameworkDescription = RuntimeInformation.FrameworkDescription,
                MemoryFootprint = (Process.GetCurrentProcess().WorkingSet64 / 1048576).ToString("N2") + " MB",
                WorkingTime = DateHelper.TimeSubTract(DateTime.Now, Process.GetCurrentProcess().StartTime)
            }, "获取服务器配置信息成功");
        }

        /// <summary>
        /// SignalR send data
        /// </summary>
        /// <returns></returns>
        // GET: api/Logs
        [HttpGet]
        public MessageModel<List<LogInfo>> Get()
        {
            //if (AppSettings.app(new string[] { "Middleware", "SignalRSendLog", "Enabled" }).ObjToBool())
            //{
            //    _hubContext.Clients.All.SendAsync("ReceiveUpdate", LogLock.GetLogData()).Wait();
            //}
            //return Success<List<LogInfo>>(null, "执行成功");

            var res = LogLock.GetLogData();
            return Success(res);
        }


    }
}
